#include "Cell.h"
#include "Diamond.h"
#include "Board.h"
#include "Utils.h"
#include "ResMgr.h"
#include "Vector.h"
#include "Animation.h"
#include "AnimationSprite.h"
#include "ZoomEffect.h"
#include "Texture.h"
#include "ResMgr.h"

Cell::Cell()
{
	diamond = 0;
	lock = 0;
	posInBoard = new Vector();
	isAvailable = true;
	SetState(CLS_NORMAL);
	tEmptyCell = ResMgr::GetInstance()->GetTexture(TID_EMPTY_CELL);
}

Cell::Cell(int lock, int xInBoard, int yInBoard, Board* board)
{
	diamond = 0;
	this->board = board;
	isAvailable = true;
	Animation* ani = new Animation(0);
	ani->GenerateAuto(ResMgr::GetInstance()->GetTexture(TID_LOCK),
		32, 32,4, 0, 3, 0);
	aniSprite->AddAnimation(ani);
	posInBoard = new Vector((float)xInBoard, (float)yInBoard);

	width = board->GetCellWidth();
	height = board->GetCellHeight();
	pos->x = board->GetPos()->x + xInBoard*width; 
	pos->y = board->GetPos()->y +yInBoard*height;
	zoomEffect = new ZoomEffect(pos->x + board->GetCellWidth()/2,
		pos->y + board->GetCellHeight()/2, 
		1, 3,	// zoom
		1, 0,	// alpha
		0.5,	// time life
		20,		// num frame
		board->GetCellWidth(), board->GetCellWidth());
	SetLock(lock);
	SetState(CLS_NORMAL);
	tEmptyCell = ResMgr::GetInstance()->GetTexture(TID_EMPTY_CELL);
}

void Cell::SetState(int state)
{
	this->state = state;

	switch(state)
	{
	case CLS_CHANGING:
		zoomEffect->Restart();
		break;
	}
}

void Cell::SetDiamond(Diamond* diamond)
{
	this->diamond = diamond;
	if(diamond!=0)
		diamond->SetCell(this);
}

void Cell::Render()
{
	if(isAvailable == true)
	{
		// lock
		if(lock!=0)
		{
			aniSprite->GetAnimation(0)->SetSpriteIndex(true, lock-1);
			aniSprite->Render(pos->x, pos->y, 1.25, 1.25, 0.5);
		}

		// caro
		else
		{
			if((int)(posInBoard->x+posInBoard->y)%2==0)
			{
				aniSprite->GetAnimation(0)->SetSpriteIndex(true, 3);
				aniSprite->Render(pos->x, pos->y, 1.25, 1.25, 0, 0.15, 0.5);
			}
		}

		//diamond
		if(diamond!=0)
			diamond->Render();

		switch(state)
		{
			// in here, lock is updated (decrease)
		case CLS_CHANGING:
			if(lock>=0 && zoomEffect->GetIsAlive() == true)
			{
				aniSprite->GetAnimation(0)->SetSpriteIndex(true, lock);
				aniSprite->Render(zoomEffect->pos->x, zoomEffect->pos->y, 
				zoomEffect->GetZoom()*1.25, 
				zoomEffect->GetZoom()*1.25, 0, zoomEffect->GetAlpha(), 0.9);
			}
			break;
		}
	}

	//if it not available
	else
	{
		/*aniSprite->GetAnimation(0)->SetSpriteIndex(true, 3);
		aniSprite->Render(pos->x, pos->y, 1.25, 1.25, 0, 1, 0.7);*/
		tEmptyCell->Render(pos->x, pos->y, 0.7);
	}
}

void Cell::Update()
{
	if(diamond!=0)
		diamond->Update();

	switch(state)
	{
	case CLS_CHANGING:
		zoomEffect->Update();
		if(zoomEffect->GetIsAlive() == false)
			SetState(CLS_NORMAL);
		break;
	}
}

void Cell::DecreaseLock()
{
	if(lock>0)
	{
		SetLock(lock-1);
		SetState(CLS_CHANGING);
	}
}

void Cell::SetLock(int lock)
{
	this->lock = lock;
	if(lock>0) 
	{
		if(lock != EMPTY_CELL_LOCK_ID)
			aniSprite->GetAnimation(0)->SetSpriteIndex(true, lock-1);
		else aniSprite->GetAnimation(0)->SetSpriteIndex(true, 3);
	}
}

Cell::~Cell()
{
	DELETE_SAFELY(posInBoard);
	DELETE_SAFELY(zoomEffect);
}

void Cell::SetIsAvailable(int isAvailable)
{
	this->isAvailable = isAvailable;
}